/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.customerManagement.controller;

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.ssau.customerManagement.model.entities.Customer;
import ru.ssau.customerManagement.model.entities.CustomerLocation;

/**
 *
 * @author illabefat
 */
public class XMLHelper {
    private static final Logger logger = LoggerFactory.getLogger(XMLHelper.class);
    
    public static String jaxbToString(Customer customer)
    {
        return anyToString(customer);        
    }
    
    public static String jaxbToString(CustomerLocation customerLocation)
    {
        return anyToString(customerLocation);        
    }
    
    public static String jaxbToString(Customer.CustomersList customers)
    {
        return anyToString(customers);        
    }
    
    public static String jaxbToString(CustomerLocation.CustomerLocationsList customerLocations)
    {
        return anyToString(customerLocations);        
    }    
    
    private static String anyToString(Object o)
    {
        JAXBContext jaxbContext1;
        try {
            jaxbContext1 = JAXBContext.newInstance(Customer.class, CustomerLocation.class,
            Customer.CustomersList.class, CustomerLocation.CustomerLocationsList.class);

            Marshaller jaxbMarshaller = jaxbContext1.createMarshaller();

            StringWriter sw = new StringWriter();
            jaxbMarshaller.marshal(o, sw);
            String xmlString = sw.toString();

            return xmlString;
        } catch (JAXBException ex) {
            logger.error("Some error ocurred during xmlization", ex);
            throw new RuntimeException(ex);
        }
    }
    
}
